package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.BuyOftenGoodsBeen;
import cn.xfdzx.android.apps.shop.bean.GetJieguoBeen;
import cn.xfdzx.android.apps.shop.bean.PosterBeen;
import cn.xfdzx.android.apps.shop.bean.UserInfoBean;
import cn.xfdzx.android.apps.shop.contract.MyInterestGoodContract;
import cn.xfdzx.android.apps.shop.model.MyInterestGoodModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class MyInterestGoodPresenter extends BasePresenter<MyInterestGoodContract.View> implements MyInterestGoodContract.Presenter {

    private MyInterestGoodContract.Model model;

    public MyInterestGoodPresenter() {
        model = new MyInterestGoodModel();
    }


    @Override
    public void myInterestGood(boolean isRefresh, boolean isLoading, String index_status, String page, String limit) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading) {
            mView.showLoading();
        }
        model.myInterestGood(index_status, page, limit)
                .compose(RxScheduler.<BuyOftenGoodsBeen>Flo_io_main())
                .as(mView.<BuyOftenGoodsBeen>bindAutoDispose())
                .subscribe(new Consumer<BuyOftenGoodsBeen>() {
                    @Override
                    public void accept(BuyOftenGoodsBeen bean) throws Exception {
                        mView.onMyInterestGoodSuccess(bean);
                        if (isLoading) {
                            mView.hideLoading();
                        }
                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading) {
                            mView.hideLoading();
                        }
                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }

    @Override
    public void getUserinfo(String sales_model) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }

        mView.showLoading();
        model.getUserinfo(sales_model)
                .compose(RxScheduler.<UserInfoBean>Flo_io_main())
                .as(mView.<UserInfoBean>bindAutoDispose())
                .subscribe(new Consumer<UserInfoBean>() {
                    @Override
                    public void accept(UserInfoBean bean) throws Exception {
                        mView.getUserInfoSuccess(bean);
                        mView.hideLoading();

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();

                    }
                });
    }

    @Override
    public void getPoster() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }

        mView.showLoading();
        model.getPoster()
                .compose(RxScheduler.<PosterBeen>Flo_io_main())
                .as(mView.<PosterBeen>bindAutoDispose())
                .subscribe(new Consumer<PosterBeen>() {
                    @Override
                    public void accept(PosterBeen bean) throws Exception {
                        mView.onPosterSuccess(bean);
                        mView.hideLoading();

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();

                    }
                });
    }

    @Override
    public void forJieGuo() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getjieguo()
                .compose(RxScheduler.<GetJieguoBeen>Flo_io_main())
                .as(mView.<GetJieguoBeen>bindAutoDispose())
                .subscribe(new Consumer<GetJieguoBeen>() {
                    @Override
                    public void accept(GetJieguoBeen bean) throws Exception {
                        mView.onJieGuo(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }
}
